/**
 * 
 */
package com.dharma.mydiet.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.dharma.mydiet.R;
import com.dharma.mydiet.storage.DAOFood;
import com.dharma.mydiet.storage.DAORegister;
import com.dharma.mydiet.storage.DAORegisterFood;
import com.dharma.mydiet.storage.DAORegisterPersonal;
import com.dharma.mydiet.storage.TransactionalRegister;
import com.dharma.mydiet.storage.TransactionalRegisterFood;
import com.dharma.mydiet.storage.TransactionalRegisterPersonal;
import com.dharma.mydiet.utility.Utils;
import com.google.analytics.tracking.android.EasyTracker;

/**
 * @author josh
 * 
 */

public class ListadoRegistrosActivity extends Activity {

    private ArrayList<TransactionalRegister> registers;
    private ArrayAdapter<CharSequence> adapterTypeRegister;
    private ArrayAdapter<CharSequence> adapterTypeListado;
    private ArrayAdapter<CharSequence> adapterPeriodos;
    private Spinner registerType;
    private Spinner periodos;
    private ListView list;

    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	this.setContentView(R.layout.activity_listado_registros);
	int title = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
	Utils.applyCustomFont((TextView) findViewById(title), getApplicationContext());
	ActionBar ab = getActionBar();
	ab.setSubtitle("Listado de Registros");
	
	
	registerType = (Spinner) findViewById(R.id.registros);
	adapterTypeRegister = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
	adapterTypeRegister.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	registerType.setAdapter(adapterTypeRegister);
	
	periodos = (Spinner) findViewById(R.id.periodo);
	adapterPeriodos = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
	adapterPeriodos.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	periodos.setAdapter(adapterPeriodos);
	adapterPeriodos.add("Ultimo dia");
	adapterPeriodos.add("Ultima semana");
	adapterPeriodos.add("Ultimo mes");
	adapterPeriodos.add("Todos los tiempos");
	
	list = (ListView) findViewById(R.id.listado);
	adapterTypeListado = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1);
	list.setAdapter(adapterTypeListado);

	adapterTypeRegister.clear();
	registers = DAORegister.getInstance().getRegisters();
	if (registers != null) {
	    adapterTypeRegister.add("Alimento");
	    for (int i = 0; i < registers.size(); i++) {
		adapterTypeRegister.add(((TransactionalRegister) registers.get(i)).getName());
	    }
	}
	registerType.setAdapter(adapterTypeRegister);
	
	periodos.setOnItemSelectedListener(new OnItemSelectedListener() {

	    @Override
	    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (registerType.getSelectedItemPosition() == 0) {
		    adapterTypeListado.clear();
		    ArrayList registers = DAORegisterFood.getInstance().getUsers();
		    if (registers != null) {
			for (int i = 0; i < registers.size(); i++) {
			    TransactionalRegisterFood register = (TransactionalRegisterFood) registers.get(i);
			    String food = (DAOFood.getInstance().getFood("" + register.getIdFoodValue())).getName();
			    String newstring = new SimpleDateFormat("dd-MM-yyyy hh:mm").format(register.getDate());
			    String value = newstring+ " " + food + " " + register.getQuantity();
			    
			    Date today = Calendar.getInstance().getTime();
			    
			    long startTime = (register.getDate()).getTime();
			    long endTime = today.getTime();
			    long diffTime = endTime - startTime;
			    long diffDays = diffTime / (1000 * 60 * 60 * 24);
			    
			    switch (arg2) {
			    case 0:
    				if (diffDays<=1){
    				    adapterTypeListado.add(value);
    				}
				break;
			    case 1:
 				if (diffDays<=7){
    				    adapterTypeListado.add(value);
    				}
				break;
			    case 2:
 				if (diffDays<=31){
    				    adapterTypeListado.add(value);
    				}
				break;
			    case 3:
				adapterTypeListado.add(value);
				break;
			    default:
				adapterTypeListado.add(value);
				break;
			    }
			}
		    } else {
			adapterTypeListado.add("No hay ningun consumo registrado");
		    }
		} else {
		    adapterTypeListado.clear();
		    int idRegisterSelected =(registers.get( registerType.getSelectedItemPosition()-1)).getIdRegister();
		    ArrayList registers = DAORegisterPersonal.getInstance().getRegistersbyIDRegistro(""+idRegisterSelected);
		    if (registers != null && registers.size()>0) {
			for (int i = 0; i < registers.size(); i++) {
			    TransactionalRegisterPersonal register = (TransactionalRegisterPersonal) registers.get(i);
			    String newstring = new SimpleDateFormat("dd-MM-yyyy hh:mm").format(register.getDate());
			    String value = newstring + " " + register.getQuantity();
			    
			    Date today = Calendar.getInstance().getTime();
			    
			    long startTime = (register.getDate()).getTime();
			    long endTime = today.getTime();
			    long diffTime = endTime - startTime;
			    long diffDays = diffTime / (1000 * 60 * 60 * 24);
			    
			    switch (arg2) {
			    case 0:
    				if (diffDays<=1){
    				    adapterTypeListado.add(value);
    				}
				break;
			    case 1:
 				if (diffDays<=7){
    				    adapterTypeListado.add(value);
    				}
				break;
			    case 2:
 				if (diffDays<=31){
    				    adapterTypeListado.add(value);
    				}
				break;
			    case 3:
				adapterTypeListado.add(value);
				break;
			    default:
				adapterTypeListado.add(value);
				break;
			    }
			}
		    } else {
			adapterTypeListado.add("No hay ningun consumo registrado");
		    }
		}
	    }

	    @Override
	    public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	    }
	});

    }

    @Override
    public void onStart() {
	super.onStart();
	EasyTracker.getInstance(this).activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
	super.onStop();
	EasyTracker.getInstance(this).activityStop(this); // Add this method.
    }
}
